﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace WorldOnFire
{
    public class CBomber : ABCEnemy
    {
        private float[] m_fOrbitTimer;

        public override void Initialize(int _nObjectCount)
        {
            // Initialize the arrays
            m_fOrbitTimer = new float[_nObjectCount];

            // Initialize base members
            base.Initialize(_nObjectCount);

            // Initialize the members
            for (int iObject = 0; iObject < _nObjectCount; ++iObject)
            {
                m_fMass = 10.0f;
                m_fEngineForce = 25.0f * m_fMass;
                m_fMaximumSpeed = 25.0f;
                m_fTurnSpeed = 1.0f * (float)Math.PI;
                m_fOrbitAngleLock = 0.9f;

                m_fMinimumRange = 75.0f;
                m_fMaximumRange = 75.0f;
                m_fShotDelayPrimary = 2.0f;
                m_fShotDelaySecondary = 0.0f;
                m_fShotSpeed = 5.0f;
                m_fDamage = 5.0f;
                m_fAreaOfEffect = 0.0f;
                m_fMaximumHealth = 25.0f;

                // TODO: SET TARGET INDEX
                m_nTargetIndex[iObject] = 0;
                m_fDisabledTimer[iObject] = 0.0f;
                m_fShotTimerPrimary[iObject] = 0.0f;
                //m_vOrbitVector[iObject] = new Vector3(0.5f, 0.5f, 0.0f);

                // TODO: LOLWUT
                this.SetAIState(iObject, eAIState.CLOSING);
            }
        }

        public override void LoadContent()
        {
            m_eModelID = CRenderingManager.eModelID.ENEMY_BOMBER;

            base.LoadContent();
        }

        protected override void FirePrimary(int _nIndex)
        {
            s_ObjectManager.GetProjectile(ABCProjectile.eProjectileType.BOMB).CreateProjectile(this, _nIndex, new Vector3(0.0f, 0.0f, 2.5f));
        }

        protected override void AIClosing(int _nIndex, float _fDeltaTime)
        {
            m_fOrbitTimer[_nIndex] = 0.0f;

            base.AIClosing(_nIndex, _fDeltaTime);
        }

        protected override void AIAttacking(int _nIndex, float _fDeltaTime)
        {
            m_fOrbitTimer[_nIndex] += _fDeltaTime;

            base.AIAttacking(_nIndex, _fDeltaTime);
        }

        protected override void CopyRenderingTransforms()
        {
            // Loop through each index for rendering
            for (int iRenderingIndex = 0; iRenderingIndex < m_nObjectsActive; ++iRenderingIndex)
            {
                int nIndex = m_ActiveIndexList[iRenderingIndex];

                // Copy the transform into the rendering transform array
                m_mRenderingTransforms[iRenderingIndex] = m_mTransform[nIndex];

                // If the ship is in orbit
                if (m_fOrbitTimer[nIndex] > 0.5f)
                {
                    Vector3 vPosition = m_mRenderingTransforms[iRenderingIndex].Translation;
                    Vector3 vTargetDirection = Vector3.Normalize(s_Target.GetPosition(m_nTargetIndex[nIndex]) - vPosition);

                    m_mRenderingTransforms[iRenderingIndex].Right 
                        = Vector3.Normalize(Vector3.Cross(-vTargetDirection, m_vVelocity[nIndex]));
                    m_mRenderingTransforms[iRenderingIndex].Backward 
                        = Vector3.Normalize(m_vVelocity[nIndex]);
                    m_mRenderingTransforms[iRenderingIndex].Up 
                        = Vector3.Normalize(Vector3.Cross(
                        m_mRenderingTransforms[iRenderingIndex].Backward, 
                        m_mRenderingTransforms[iRenderingIndex].Right));
                }
            }
        }
    }
}
